package gui;

import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import net.miginfocom.swing.MigLayout;

/********************************************************
 *
 *  Project :  TeamProject
 *  Package :  gui
 *  File    :  HelpPanel.java
 *  Name    :  Tyler Daines
 *  Date    :  Nov 25, 2013
 *
 ********************************************************/
public class HelpPanel extends JPanel implements MouseListener, KeyListener
{
	
	private JList<JLabel> helpItems;
	private JLabel pleaseChoose;
	
	private JLabel addingRecipe, deletingRecipe, editingRecipe, 
	searchRecipe, saveRecipe, saveToHD, printingRecipe,addPicture, 
	creatingMeal, favorites;
	
	private JTextArea viewArea;
	private JScrollPane scrollArea;
	
	/********************************************************
	 * Name       : HelpPanel
	 * Purpose    : Constructor
	 * Parameters : 
	 ********************************************************/
	public HelpPanel()
	{
		setLayout(new MigLayout());
		
		initLabel();
		initPanels();
		initList();
		initArea();
		addComponets();
	}

	/********************************************************
	* Name       : initLabels
	* Purpose    : 
	* Parameters : 
	* Returns    : void
	********************************************************/
	private void initLabel()
	{
		pleaseChoose = new JLabel("Please Choose a Topic:");
	}
	
	/********************************************************
	* Name       : initPanels
	* Purpose    : 
	* Parameters : 
	* Returns    : void
	********************************************************/
	private void initPanels()
	{
		String addingRecipeString = "To add a recipe to the recipe box click the \"Add Recipe\" button or choose \"New Recipe\" from the \"File\" menu. "
				+ "Proceed by filling out all the required fields for the recipe and clicking the \"Save\" button. The required "
				+ "fields are the \"Name\" field, the Name field for each ingredient and the \"Directions\" field";
		
		addingRecipe = new HelpItemLabel(addingRecipeString,"Adding a recipe");
		
		String deletingRecipeString = "To delete a recipe you must have a recipe open and click the \"Delete\" button once"
				+ " confirmed the recipe will be removed from memory permanently";
		
		deletingRecipe = new HelpItemLabel(deletingRecipeString, "Deleting a recipe");
		
		String editingRecipeString = "To edit a recipe open the recipe from either the \"Home\" view or \"Search\" view "
				+ "and proceed to edit the fields. Click the \"Save\" button to save the changes or the \"Cancel\" button to"
				+ " discard changes.";
		
		editingRecipe = new HelpItemLabel(editingRecipeString, "Editing a recipe");
		
		String searchRecipeString = "To search for a recipe click on the \"Search Recipes\" button or \"Search\" from the \"File\" menu. "
				+ "Proceed by filling out the field and clicking the \"Search by Name\" or \"Search by Ingredient\" buttons. Selecting "
				+ "an item in the Category drop down menu will also filter the results by category. To search by category alone select a"
				+ " category and click the \"Search by Category\" button.  The fields allow the user to enter full or partial desired searches.";
		
		searchRecipe = new HelpItemLabel(searchRecipeString, "Searching for a recipe");
		
		String saveRecipeString = "To save, have a recipe open and click on the \"Save\" button if the recipe already exists in the database "
				+ "a you will be asked to confirm overwriting the recipe.";
		
		saveRecipe = new HelpItemLabel(saveRecipeString, "Saving a recipe");
		
		String saveToHDString = "To save the recipe outside the program you must be in the recipe view. Select \"Save as...\" from the \"File\" " +
				"menu and choose a name and desitnation.";
		
		saveToHD = new HelpItemLabel(saveToHDString, "Saving a recipe to hard drive or external memory");
		
		String printingRecipeString = "To print a recipe have a recipe open and click the \"Print\" button.";
		
		printingRecipe = new HelpItemLabel(printingRecipeString, "Printing a recipe");
		
		String addPictureString = "To add a picture to a recipe have it open and double click the current picture. If you "
				+ "choose Update you will be asked for a new photo. If you choose remove the default picture will take it's "
				+ "place. If you choose cancel nothing will happen.";
		
		addPicture = new HelpItemLabel(addPictureString, "Adding a picture to a recipe");
		
		String creatingMealString = "Go to the meal bundle view by either selecting the meal bundle tab or selecting the \"Meal\" button.  Enter the name" +
				"of the Meal bundle in the field.  Choose the category you are looking for or select a recipe from the list.  You can " +
				"add or remove the recipes from the meal bundle by selecting the recipe and cliking the arrows.  Once you have all the recipes chosen hit " +
				"save and the meal bundle will be added to the meal bundle drop down located next to the name field.";
		
		creatingMeal = new HelpItemLabel(creatingMealString, "Creating a meal plan");
		
		String favoritesString = "To add a favorite to the \"Favorites List\" on the home page check the \"Favorite\" check box on the recipe and save it.";
		
		favorites = new HelpItemLabel(favoritesString, "Creating a favorites list");
		
		
	}

	/********************************************************
	* Name       : initList
	* Purpose    : 
	* Parameters : 
	* Returns    : void
	********************************************************/
	private void initList()
	{
		JLabel[] labels = {addingRecipe, deletingRecipe, editingRecipe, 
				searchRecipe, saveRecipe, saveToHD, printingRecipe,addPicture, 
				creatingMeal, favorites};
		
		helpItems = new JList<>(labels);
		helpItems.addMouseListener(this);
		helpItems.addKeyListener(this);
		
	}
	
	/********************************************************
	* Name       : initArea
	* Purpose    : 
	* Parameters : 
	* Returns    : void
	********************************************************/
	private void initArea()
	{
		viewArea = new JTextArea();
		viewArea.setWrapStyleWord(true);
		viewArea.setLineWrap(true);
		
		scrollArea = new JScrollPane();
		scrollArea.setMinimumSize(new Dimension(300,400));
		scrollArea.setMaximumSize(new Dimension(300,400));
		scrollArea.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		scrollArea.setViewportView(viewArea);
	}
	
	/********************************************************
	* Name       : addComponets
	* Purpose    : 
	* Parameters : 
	* Returns    : void
	********************************************************/
	private void addComponets()
	{
		add(pleaseChoose, "cell 0 0");
		add(helpItems, "cell 0 1");
		add(scrollArea, "cell 1 1");
	}
	

	@Override
	public String getName()
	{
		return "Help";
	}

	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseClicked(MouseEvent e)
	{
		if(e.getSource() == helpItems)
		{
			viewArea.setText(helpItems.getSelectedValue().getText());
			scrollArea.setViewportView(viewArea);
			validate();
		}
		
	}

	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseEntered(MouseEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseExited(MouseEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
	 */
	@Override
	public void mousePressed(MouseEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseReleased(MouseEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
	 */
	@Override
	public void keyPressed(KeyEvent e)
	{
//		if((e.getKeyCode() == KeyEvent.VK_UP))
//		{
//			if(e.getSource() == helpItems)
//			{
//				viewArea.setText(helpItems.getSelectedValue().getText());
//				scrollArea.setViewportView(viewArea);
//				validate();
//			}
//		}
//		
//		if((e.getKeyCode() == KeyEvent.VK_DOWN))
//		{
//			if(e.getSource() == helpItems)
//			{
//				viewArea.setText(helpItems.getSelectedValue().getText());
//				scrollArea.setViewportView(viewArea);
//				validate();
//			}
//		}
		
	}

	/* (non-Javadoc)
	 * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
	 */
	@Override
	public void keyReleased(KeyEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
	 */
	@Override
	public void keyTyped(KeyEvent e)
	{
		// TODO Auto-generated method stub
		
	}
}
